package org.agridata.akvisualization.service.impl;

import org.agridata.akvisualization.doMain.entity.ResourceUtilization;
import org.agridata.akvisualization.mapper.ResourceUtilizationMapper;
import org.agridata.akvisualization.result.Result;
import org.agridata.akvisualization.service.ResourceUtilizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * @author: yxc
 * @date: 2024/2/20 15:21
 * @Description: 资源化利用
 */
@Service
public class ResourceUtilizationServiceImpl implements ResourceUtilizationService {

    @Autowired
    ResourceUtilizationMapper resourceUtilizationMapper;

    @Override
    public Result importResourceUtilization(List<ResourceUtilization> memberList, String dates, String monthly) {
        int successCount = 0;
        int failureCount = 0;
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将LocalDateTime格式化为字符串
        String formattedTime = currentTime.format(formatter);

        List<Map<String,Object>> SaveCrop = resourceUtilizationMapper.selectResourceUtilization(dates, monthly);
        if (SaveCrop.size() > 0) {
            resourceUtilizationMapper.deleteResourceUtilization(SaveCrop);
        }

        for (ResourceUtilization animalSaveCrop : memberList) {
            try {
                animalSaveCrop.setDate(dates);
                animalSaveCrop.setMonthly(monthly);
                animalSaveCrop.setCreateAt(formattedTime);
                resourceUtilizationMapper.insertResourceUtilization(animalSaveCrop);
                successCount++;
            } catch (Exception e) {
                failureCount++;
                e.printStackTrace();
            }
        }
        return Result.ok("成功："+ successCount+ "条"+"，"+"失败"+failureCount+"条");
    }

    @Override
    public List<ResourceUtilization> getResourceList(String data, String monthly) {
        //判断 长度是不是2 如果不是的话补0
        if (monthly.length() < 2){
            monthly = "0"+ monthly;
        }
        return resourceUtilizationMapper.selectResourceList(data,monthly);
    }

    @Override
    public List<Map<String, Object>> getResource(String data, String monthly) {
        return resourceUtilizationMapper.getResource(data,monthly);
    }

    @Override
    public Result getResourceHomePage(String monthly) {
        Integer integer = Integer.valueOf(monthly);
        Map<String, Object> resourceHomePage = resourceUtilizationMapper.getResourceHomePage(integer);
        return Result.ok(resourceHomePage);
    }


}
